package cn.com.zzl.util;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;

import java.util.List;

/**
 * Description：自定义响应结构
 * Created by 朱正磊 on 2017-06-30 18:06.
 */
public class ResponseResult {

	/**
	 * @Desc 定义jackson对象
	 */
	private static final ObjectMapper MAPPER = new ObjectMapper();

	/**
	 * @Desc 响应业务状态
	 */
	private Integer status;

	/**
	 * @Desc 响应消息
	 */
	private String msg;

	/**
	 * @Desc 响应中的数据
	 */
	private Object data;

	public ResponseResult() {}

	private ResponseResult(Integer status, String msg, Object data) {
		this.status = status;
		this.msg = msg;
		this.data = data;
	}

	/**
	 * @Desc 构造函数设置响应业务状态
	 * @param status 响应业务状态
	 * @return
	 */
	public static ResponseResult buildResult(Integer status){
		return new ResponseResult(status, null, null);
	}

	/**
	 * @Desc 构造函数只设置响应消息
	 * @param msg 响应消息
	 * @return
	 */
	public static ResponseResult buildResult(String msg){
		return new ResponseResult(null, msg, null);
	}

	/**
	 * @Desc 构造函数同时设置响应业务状态和响应消息
	 * @param status 响应业务状态
	 * @param msg 响应消息
	 * @return
	 */
	public static ResponseResult buildResult(Integer status, String msg) {
		return new ResponseResult(status, msg, null);
	}

	/**
	 * @Desc 构造函数同时设置响应业务状态、响应消息、响应数据
	 * @param status 响应业务状态
	 * @param msg 响应消息
	 * @param data 响应数据
	 * @return
	 */
	public static ResponseResult buildResult(Integer status, String msg, Object data) {
		return new ResponseResult(status, msg, data);
	}

	/**
	 * ResponseResult的json字符串转ResponseResult对象
	 * @Desc data为null直接将json字符串转pojo对象
	 * @param json
	 * @return
	 */
	public static ResponseResult format(String json) {
		try {
			//json字符串中data为null，将json字符串转换成ResponseResult对象
			return MAPPER.readValue(json, ResponseResult.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * ResponseResult的json字符串转ResponseResult对象
	 * @Desc data为json字符串转pojo对象
	 * @param json json数据
	 * @param clazz ResponseResult中的object类型
	 * @return
	 */
	public static ResponseResult formatToPojo(String json, Class<?> clazz) {
		try {
			if (clazz == null) {
				//如果json字符串中data为null，将json字符串转换成ResponseResult对象
				return MAPPER.readValue(json, ResponseResult.class);
			}
			JsonNode jsonNode = MAPPER.readTree(json);
			//获取data的值
			JsonNode data = jsonNode.get("data");
			//obj用来存储data
			Object obj = null;
			if (data.isObject()) {//如果data是一个pojo对象
				obj = MAPPER.readValue(data.traverse(), clazz);
			} else if (data.isTextual()) {//如果data是一个纯字符串
				obj = MAPPER.readValue(data.asText(), clazz);
			}
			//将ResponseResult的json字符串格式转换成ResponseResult对象
			return buildResult(jsonNode.get("status").getIntValue(), jsonNode.get("msg").asText(), obj);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * ResponseResult的json字符串转ResponseResult对象
	 * @Desc data为json字符串转pojo的集合对象
	 * @param json json数据
	 * @param clazz 集合中的类型
	 * @return
	 */
	public static ResponseResult formatToList(String json, Class<?> clazz) {
		try {
			if (clazz == null) {
				//如果json字符串中data为null，将json字符串转换成ResponseResult对象
				return MAPPER.readValue(json, ResponseResult.class);
			}
			JsonNode jsonNode = MAPPER.readTree(json);
			//获取data的值
			JsonNode data = jsonNode.get("data");
			//obj用来存储data
			Object obj = null;
			if (data.isArray() && data.size() > 0) {//如果data是数组格式并且长度不为0
				obj = MAPPER.readValue(data.traverse(), MAPPER.getTypeFactory()
						.constructCollectionType(List.class, clazz));
			}
			//将ResponseResult的json字符串格式转换成ResponseResult对象
			return buildResult(jsonNode.get("status").getIntValue(), jsonNode.get("msg").asText(), obj);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public Integer getStatus() {
		return status;
	}
	public void setStatus(Integer status) {
		this.status = status;
	}

	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Object getData() {
		return data;
	}
	public void setData(Object data) {
		this.data = data;
	}

}
